﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Reflection;
using DataSync.BLL;
using DataSync.Model;

namespace DataSync
{
    public partial class CustomizeSync : Form
    {
        private Thread customizeSyncThread;
        private CustomizeSyncService service;
        private CustomizeSyncPara threadPara;

        public CustomizeSync()
        {
            InitializeComponent();
            InitObject();
        }

        private void InitObject()
        {
            service = new CustomizeSyncService();
            service.LbMsg = this.lbSyncMsg;
            threadPara = new CustomizeSyncPara();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("将覆盖已有数据，是否继续？", "警告", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            threadPara.obj = (SyncBase)this.cmbTableList.SelectedItem;
            threadPara.startDate = DateTime.Parse(this.dtpStartDate.Text).AddDays(-1);
            threadPara.endDate = DateTime.Parse(this.dtpEndDate.Text).AddDays(1);

            if (customizeSyncThread == null)
            {
                customizeSyncThread = new Thread(new ParameterizedThreadStart(CustomizeSyncService.ProcessData));

                customizeSyncThread.Start(threadPara);
                customizeSyncThread.IsBackground = true;
            }
            else
            {
                if (customizeSyncThread.ThreadState == ThreadState.Running)
                {
                    MessageBox.Show("同步程序正在运行，请耐心等待程序完成。", "警告", MessageBoxButtons.OK);
                    return;
                }

                else if (customizeSyncThread.ThreadState == ThreadState.Stopped)
                {
                    customizeSyncThread.Abort();
                    customizeSyncThread.Join();
                    customizeSyncThread = new Thread(new ParameterizedThreadStart(CustomizeSyncService.ProcessData));

                    customizeSyncThread.Start(threadPara);
                    customizeSyncThread.IsBackground = true;
                }
            }
        }

        private void CustomizeSync_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (customizeSyncThread == null)
                return;
            else if (customizeSyncThread.ThreadState == (ThreadState.Running | ThreadState.Background))
            {
                if (MessageBox.Show("你确定要退出吗？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        protected void BindTableList()
        {
            Type tables = typeof(DateIncrementSyncTable);
            ArrayList al = new ArrayList();

            ConcreteCreator creator = new ConcreteCreator();

            foreach (int index in Enum.GetValues(tables))
            {
                //if (index != 100 && index != 101)
                    al.Add(creator.CreateDateIncrementSyncTable(index));
            }

            tables = typeof(FullSyncTables);
            foreach (int index in Enum.GetValues(tables))
            {
                //if (index == 5 || index == 6 || index == 40)
                    al.Add(creator.CreateFullSyncTable(index));
            }

            this.cmbTableList.DataSource = al;

            if (al.Count > 0)
            {
                this.cmbTableList.DisplayMember = "sourceTableName";
            }
        }

        private void CustomizeSync_Load(object sender, EventArgs e)
        {
            BindTableList();
        }

        private void CustomizeSync_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (customizeSyncThread != null)
                customizeSyncThread.Abort();

            this.Dispose();
        }

        private void cmbTableList_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (((SyncBase)this.cmbTableList.SelectedItem).SyncType)
            {
                case 0:
                    this.dtpStartDate.Enabled = false;
                    this.dtpEndDate.Enabled = false;
                    break;
                case 1:
                    this.dtpStartDate.Enabled = true;
                    this.dtpEndDate.Enabled = true;
                    break;
            }
        }
    }
}
